/*
 +----------------------------------------------------------------------
 + Title        : tools
 + Author       : huwl
 + Version      : 1.0.0
 + Initial-Time : 2023-08-10
 + Last-time    : 这个文件最后修改的时间 + 修改人的名称
 + Desc         : 工具集合
 +----------------------------------------------------------------------
*/

import dayjs from "dayjs"
// npm i dayjs

/**
 * 格式化时间
 * 
 * @param string time ---------------------------必传
 * @param string format -------------------------必传
 * @returns string
 * @author huwl
 */
export const formatTime = (time: string, format: string) => {
    return dayjs(time).format(format)
}

const simplifyDate = [
    '今天',
    '昨天',
    '前天',
    '前1天',
    '前2天',
    '前3天',
    '前4天',
    '前5天',
    '前6天',
    '前7天',
]

/**
 * 简化时间格式
 * 
 * @param string time ---------------------------必传
 * @returns string
 * @author huwl
 */
export const simplifyDateFormat = (time: string) => {
    if (time == '' || time == undefined) {
        return ''
    }
    const nowTime = dayjs()
    const date = dayjs(time).format('YYYY-MM-DD')
    let result = dayjs(time).format('YYYY-MM-DD')

    for (let index = 0; index < 8; index++) {
        if (date == nowTime.subtract(index, 'day').format('YYYY-MM-DD')) {
            result = index == 0 ? dayjs(time).format('HH:mm') : simplifyDate[index]
        }
    }

    return result
}

/**
 * 去除最后一个逗号
 * 
 * @param string str ----------------------------字符串
 * @returns string
 * @author huwl
 */
export const removeLastComma = (str: string) => {
    return str.replace(/,$/gi, "");
}